package com.ctshk.rpc.airexpress.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.SystemError;
import com.ctshk.common.exception.BusinessException;
import com.ctshk.common.utils.EntityUtil;
import com.ctshk.common.utils.SnowflakeIdWorker;
import com.ctshk.rpc.airexpress.dto.AirportExpressInitializationSettingDTO;
import com.ctshk.rpc.airexpress.dto.AirportExpressInitializationSettingsChannelDTO;
import com.ctshk.rpc.airexpress.entity.AirportExpressInitializationSettings;
import com.ctshk.rpc.airexpress.entity.AirportExpressInitializationSettingsChannel;
import com.ctshk.rpc.airexpress.enums.Channel;
import com.ctshk.rpc.airexpress.mapper.AirportExpressInitializationSettingsChannelMapper;
import com.ctshk.rpc.airexpress.mapper.AirportExpressInitializationSettingsMapper;
import com.ctshk.rpc.airexpress.req.AirportExpressInitializationSettingsReq;
import com.ctshk.rpc.airexpress.service.IAirportExpressInitializationSettingsService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 机场快线api初始化设置表 服务实现类
 * </p>
 *
 * @author 谢诗宏
 * @since 2021-02-18
 */
@DubboService
public class AirportExpressInitializationSettingsServiceImpl extends ServiceImpl<AirportExpressInitializationSettingsMapper, AirportExpressInitializationSettings> implements IAirportExpressInitializationSettingsService {
    @Autowired
    AirportExpressInitializationSettingsMapper airportExpressInitializationSettingsMapper;

    @Autowired
    AirportExpressInitializationSettingsChannelMapper airportExpressInitializationSettingsChannelMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result save(AirportExpressInitializationSettingsReq airportExpressInitializationSettingsReq, Long userId) {
        AirportExpressInitializationSettings airportExpressInitializationSettingsTemp = airportExpressInitializationSettingsMapper.selectById(airportExpressInitializationSettingsReq.getId());
        if (airportExpressInitializationSettingsTemp == null) {
            // 机场快线初始化设置还未初始化
            throw new BusinessException(SystemError.AIREXPRESS_ORDER_60034);
        }
        // 构建机场快线-初始化设置
        AirportExpressInitializationSettings airportExpressInitializationSettings = EntityUtil.copy(airportExpressInitializationSettingsReq, AirportExpressInitializationSettings.class);
        airportExpressInitializationSettings.setModifiedId(userId);
        airportExpressInitializationSettings.setGmtModified(LocalDateTime.now());

        // 更新机场快线-初始化设置
        airportExpressInitializationSettingsMapper.updateById(airportExpressInitializationSettings);
        // 更新机场快线-渠道设置
        airportExpressInitializationSettingsChannelMapper.deleteAll();
        if (CollectionUtils.isNotEmpty(airportExpressInitializationSettingsReq.getAirportExpressInitializationSettingsChannelList())) {
            List<AirportExpressInitializationSettingsChannel> airportExpressInitializationSettingsChannels = EntityUtil.copyList(airportExpressInitializationSettingsReq.getAirportExpressInitializationSettingsChannelList(), AirportExpressInitializationSettingsChannel.class);
            for (AirportExpressInitializationSettingsChannel channel : airportExpressInitializationSettingsChannels) {
                if (channel.getChannelId() == null) {
                    continue;
                }
                if (channel.getId() == null) {
                    channel.setId(SnowflakeIdWorker.nextId());
                    String name = Channel.getById(channel.getChannelId().intValue()).getName();
                    channel.setChannelName(name);
                }
                channel.setSettingId(airportExpressInitializationSettings.getId());
            }
            airportExpressInitializationSettingsChannelMapper.insertBatchSomeColumn(airportExpressInitializationSettingsChannels);
        }
        return Result.success();
    }

    @Override
    public Result<AirportExpressInitializationSettingDTO> selectInfo() {
        // 查询机场快线-初始化设置
        List<AirportExpressInitializationSettings> airportExpressInitializationSettingsList = airportExpressInitializationSettingsMapper.selectList(null);
        if (CollectionUtils.isEmpty(airportExpressInitializationSettingsList)) {
            // 机场快线初始化设置还未初始化
            throw new BusinessException(SystemError.AIREXPRESS_ORDER_60034);
        }
        AirportExpressInitializationSettings airportExpressInitializationSettings = airportExpressInitializationSettingsList.get(0);
        AirportExpressInitializationSettingDTO airportExpressInitializationSettingDTO = EntityUtil.copy(airportExpressInitializationSettings, AirportExpressInitializationSettingDTO.class);
        // 查询机场快线-初始化设置渠道
        QueryWrapper<AirportExpressInitializationSettingsChannel> channelWrapper = new QueryWrapper<>();
        channelWrapper.lambda().eq(AirportExpressInitializationSettingsChannel::getSettingId, airportExpressInitializationSettings.getId());
        List<AirportExpressInitializationSettingsChannel> airportExpressInitializationSettingsChannelList = airportExpressInitializationSettingsChannelMapper.selectList(channelWrapper);
        List<AirportExpressInitializationSettingsChannelDTO> airportExpressInitializationSettingsChannelDTOList = EntityUtil.copyList(airportExpressInitializationSettingsChannelList, AirportExpressInitializationSettingsChannelDTO.class);
        airportExpressInitializationSettingDTO.setAirportExpressInitializationSettingsChannelList(airportExpressInitializationSettingsChannelDTOList);
        return Result.success(airportExpressInitializationSettingDTO);
    }
}
